package org.imlinker.plugin.say;

import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.imlinker.plugin.Say;

public class AccountManager {

	public AccountManager(Say say) {
		int size = say.getElementSize("account");
		String accountLabel = "account.account";
		for (int i = 0; i < size; i++) {
			String type = say.getValue(accountLabel + i + ".type");
			String user = say.getValue(accountLabel + i + ".username");
			String password = say.getValue(accountLabel + i + ".password");

			Account account = new Account(type, user, password);
			accounts.add(account);
		}

	}

	public boolean noAccount() {
		return accounts.isEmpty();
	}

	public void getFirst() {
		//AccountWindow accountWindow = new AccountWindow(this);
	}

	//private Say say;

	private ArrayList<Account> accounts = new ArrayList<Account>();

	public static void getAccountToSay(Say say, String oldUsername,
			String oldPassword) {
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(3, 2));
		panel.add(new JLabel("UserName:"));
		JTextField userField = new JTextField(15);
		userField.setText(oldUsername);
		panel.add(userField);
		panel.add(new JLabel("Password:"));
		JPasswordField passField = new JPasswordField(15);
		passField.setText(oldPassword);
		panel.add(passField);
		panel.add(new JLabel(""));
		JCheckBox box = new JCheckBox("Save password?", false);
		panel.add(box);
		int ret = JOptionPane.showConfirmDialog(null, panel, "Input account",
				JOptionPane.OK_CANCEL_OPTION);
		if (ret == JOptionPane.CANCEL_OPTION) {
			return;
		}

		String username = userField.getText();
		String password = new String(passField.getPassword());
		say.setUser(username);
		say.setPassword(password);

	}
}
